﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bmz.Laboratory.DesignPattern
{
    class MediatorDesignPattern
    {
        #region 配对对象案例http://blog.csdn.net/liuwenbo0920/article/details/7275911
        public abstract class Person
        {
            private String name;
            private int condition;
            private Mediator mediator;

            public Person(String name, int condition, Mediator mediator)
            {
                this.name = name;
                this.condition = condition;
                this.mediator = mediator;
            }

            public Mediator getMediator()
            {
                return mediator;
            }

            public void setMediator(Mediator mediator)
            {
                this.mediator = mediator;
            }

            public String getName()
            {
                return name;
            }
            public void setName(String name)
            {
                this.name = name;
            }
            public int getCondition()
            {
                return condition;
            }

            public void setCondition(int condition)
            {
                this.condition = condition;
            }

            public abstract void getPartner(Person person);

        }

        public class Mediator
        {
            private Man man;
            private Woman woman;

            public void setMan(Man man)
            {
                this.man = man;
            }
            public void setWoman(Woman woman)
            {
                this.woman = woman;
            }

            public void getPartner(Person person)
            {
                //将搭档设置上   
                if (person.GetType() == typeof(Man))
                {
                    this.setMan((Man)person);
                }
                else
                {
                    this.setWoman((Woman)person);
                }
                //判断条件   
                if (man == null || woman == null)
                {
                    Console.WriteLine("汗，我不是同性恋!");
                }
                else
                {

                    if (man.getCondition() == woman.getCondition())
                    {
                        Console.WriteLine(man.getName() + "和" + woman.getName() + "绝配");
                    }
                    else
                    {
                        Console.WriteLine(man.getName() + "和" + woman.getName() + "不相配");
                    }
                }
            }

        }
        public class Man : Person
        {

            public Man(String name, int condition, Mediator mediator)
                : base(name, condition, mediator)
            {

            }

            public override void getPartner(Person person)
            {
                this.getMediator().setMan(this);
                this.getMediator().getPartner(person);
            }
        }

        public class Woman : Person
        {

            public Woman(String name, int condition, Mediator mediator)
                : base(name, condition, mediator)
            {

            }

            public override void getPartner(Person person)
            {
                this.getMediator().setWoman(this);
                this.getMediator().getPartner(person);
            }

        }
        public class MainClass
        {
            public static void main(String[] args)
            {
                Mediator mediator = new Mediator();
                Person zhangsan = new Man("张三", 7, mediator);
                Person lisi = new Man("李四", 7, mediator);
                Person xiaofang = new Woman("小芳", 7, mediator);

                zhangsan.getPartner(lisi);

                xiaofang.getPartner(lisi);
                Console.Read();
            }
        }
        #endregion
    }
}
